import request from '@/utils/request'
import qs from 'qs'

export function login(data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/connect/token',
    method: 'post',
    data: qs.stringify(data),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

export function getInfo() {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: `/api/authserver/customProfile`,
    method: 'get'
  })
}

export function logout() {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/authserver/accounts/logout',
    method: 'get'
  })
}

export function getUsers(data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/identity/users',
    method: 'get',
    params: data
  })
}

export function createUser(data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/identity/users',
    method: 'post',
    data
  })
}

export function getUser(id) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/identity/users/' + id,
    method: 'get'
  })
}

export function updateUser(id, data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/identity/users/' + id,
    method: 'put',
    data
  })
}

export function deleteUser(id) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/identity/users/' + id,
    method: 'delete'
  })
}

export function getUserRoles(id) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/identity/users/' + id + '/roles',
    method: 'get'
  })
}

export function updateAvatar(data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: `/api/authserver/customProfile`,
    method: 'put',
    data
  })
}

export function removeOrganizationUnit(id, organizationUnitId) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: `/api/identity/users/${id}/organization-units/${organizationUnitId}`,
    method: 'delete'
  })
}

export function updateProfile(data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/identity/my-profile',
    method: 'put',
    data
  })
}

export function updatePassword(data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/identity/my-profile/change-password',
    method: 'post',
    data
  })
}

export function forgetPassword(data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/universityWater/sendPasswordResetCode',
    method: 'post',
    data
  })
}

export function changePasswordVerificationCode(data) {
  return request({
    baseURL: process.env.VUE_APP_AUTH,
    url: '/api/universityWater/setNewPassword',
    method: 'put',
    data
  })
}
